<?
@session_start();

require_once("conn.php");

$cmd = $_REQUEST["cmd"];

if($cmd == "edit"){
	$id = $_REQUEST["id"];
	$sql = "SELECT * FROM pch_dealer WHERE id='".$id."';";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
}
?>
<script>
	function saveEditDealer(id){
		var name = $("#dName").val();
		var tel = $("#dTel").val();
		var fax = $("#dFax").val();

		var data = "&id="+id+"&name="+name+"&tel="+tel+"&fax="+fax+"&t="+Math.random();
		
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=saveEditDealer",
		   data: data,
		   success: function(msg){
			    alert(msg);
				if(msg.match("Saved")){
					dealerManage();
				}
		   }
		});
	}

	function saveNewDealer(){
		var name = $("#dName").val();
		var tel = $("#dTel").val();
		var fax = $("#dFax").val();

		var data = "&name="+name+"&tel="+tel+"&fax="+fax+"&t="+Math.random();
		
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=saveNewDealer",
		   data: data,
		   success: function(msg){
			    alert(msg);
				if(msg.match("Saved")){
					dealerManage();
				}
		   }
		});
	}
</script>
<table width="300">
	<tr>
		<td>Dealer Name : </td>
		<td><input type="text" id="dName" value="<?=$row["dealer"]?>"></td>
	</tr>
	<tr>
		<td>Tel : </td>
		<td><input type="text" id="dTel" value="<?=$row["tel"]?>"></td>
	</tr>
	<tr>
		<td>Fax : </td>
		<td><input type="text" id="dFax" value="<?=$row["fax"]?>"></td>
	</tr>
	<tr>
		<td colspan="2">
			<?
			if($cmd == "edit"){
			?>
			<input type="button" onclick="saveEditDealer('<?=$id?>')" value="Save">
			<?
			}else{
			?>
			<input type="button" onclick="saveNewDealer()" value="Save">
			<?
			}
			?>
		</td>
	</tr>
</table>